package com.cxdr.awsips.packing.processor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.cxdr.awsips.packing.bean.CheckLog;
import com.cxdr.awsips.packing.bean.FileType;
import com.cxdr.awsips.packing.bean.PackLog;
import com.cxdr.awsips.packing.config.SystemConfig;
import com.cxdr.awsips.packing.logger.LogFactory;
import com.cxdr.awsips.packing.pool.BaseQueue;

/**
 * 文件检查器
 * @author pugang
 *
 */
public class FileChecker extends BaseProcessor {
	private static Logger logger = LogFactory.makeNewLogger(FileChecker.class);
	public static final BaseQueue<String> FileQueue = new BaseQueue<String>();
	private static final String ERROR_FILE_PATH = SystemConfig.get("path.error");
	public static final String SUCCESS = "Y";
	public static final String FAIL = "E";
	@Override
	public void run() {
		while(!isFinish()){
			String fileName = FileQueue.take();
			if(fileName==null){
				continue;
			}
			checkFile(fileName);
		}
	}
	
	private void checkFile(String fileName){
		logger.info("检查文件:"+fileName);
		CheckLog log = new CheckLog(fileName);
		log.setError_file_path(ERROR_FILE_PATH);
		log.setCheck_desc(null);
		log.setCheck_result(SUCCESS);
		if(checkFileName(log)&&checkData(log)){
			//检查成功就通知打包
			//这里直接生成Packlog 是为了避免重复计算属性
			PackLog packLog = new PackLog();
			packLog.setCreate_time(new Date());
			packLog.setCreate_day(log.getCreate_day());
			packLog.setCreate_hour(log.getCreate_hour());
			packLog.setSource_file(log.getFile_name());
			packLog.setStation_ID(log.getStation_ID());
			packLog.setPartition_month(log.getPartition_month());
			packLog.setFile_type(log.getFile_type());
			logger.info(log.getFile_name()+"送入打包线程");
			FilePacker.FileQueue.add(packLog);
			//通知入库
			logger.info("检查文件完成");
			
		}
	}
	
	private boolean checkFileName(CheckLog log){
		String fileName = log.getFile_name();
		logger.info("检查名字");
		if(fileName.contains("BENN")){
			log.setCheck_desc("包文件不打包");
			logger.info("包文件不打包");
			return false;
		}
		if(fileName.contains("CC")){
			log.setCheck_desc("更新文件不打包");
			logger.info("更新文件不打包");
			return false;
		}
		if(log.getFile_type()==null||!FileType.contained(log.getFile_type())){
			logger.info("文件类型是空 或者文件类型不对："+log.getFile_type());
			return false;
		}
		return true;
	}
	private boolean checkData(CheckLog log){
		return true;
	}
//	private boolean check
}
